from datetime import datetime

from pony import orm

from .config import settings

# import pytz
# from .config import settings2

# orm.set_sql_debug(True)


db = orm.Database()
db.bind(
    provider='postgres',
    host=settings.PG_HOST,
    port=settings.PG_PORT,
    database=settings.PG_DATABASE,
    user=settings.PG_USERNAME,
    password=settings.PG_PASSWORD,
)



################
# place all entities below
################

# Required, 必须有值的字段, 值不能是null或者''
# Optional, 值可以为null或者''的字段, 字符字段默认为'', 其它字段默认为null


class ReportFrom(db.Entity):
    _table_ = 'reportfrom'

    id = orm.PrimaryKey(int)
    task_id = orm.Optional(str)
    task_type = orm.Required(str)
    task_elapse = orm.Optional(float)
    alg_result = orm.Required(orm.Json)
    create_time = orm.Required(datetime)
    img_path = orm.Optional(str)


class SystemPerm(db.Entity):
    _table_ = 'system_perm'

    id = orm.PrimaryKey(str)
    name = orm.Optional(str)


class SystemRole(db.Entity):
    _table_ = 'system_role'

    id = orm.PrimaryKey(str)
    name = orm.Optional(str)


class SystemRolePerm(db.Entity):
    _table_ = 'system_role_perm'

    role_id = orm.PrimaryKey(str)
    perm_id = orm.Optional(str)



class SystemUserBusinessRole(db.Entity):
    _table_ = 'system_user_business_role'

    user_business_id = orm.Optional(int)
    role_id = orm.Optional(int)
    user_id = orm.Optional(str)


class SystemUserBusiness(db.Entity):
    _table_ = 'system_user_business'

    id = orm.PrimaryKey(int)
    user_id = orm.Optional(str)
    business_id = orm.Optional(int)



class SystemUserTeam(db.Entity):
    _table_ = 'system_user_team'

    user_id = orm.Optional(str)
    team_id = orm.Optional(str)


class Team(db.Entity):
    _table = 'team'

    id = orm.PrimaryKey(str)
    team_name = orm.Optional(str)


class SystemUser(db.Entity):
    _table_ = 'system_user'

    id = orm.PrimaryKey(str)
    name = orm.Optional(str)


class WorkOrder(db.Entity):
    _table_ = 'work_order'

    claim_no = orm.Optional(str)
    group = orm.Optional(str)
    assignee = orm.Optional(str)
    type = orm.Optional(str)
    state = orm.Optional(str)
    create_time = orm.Optional(datetime)
    start_time = orm.Optional(datetime)
    over_time = orm.Optional(datetime)
    group = orm.Optional(str)
    assignee = orm.Optional(str)


class CaseInfo(db.Entity):
    _table_ = 'case_info'

    batch_id = orm.Optional(str)
    claim_no = orm.Optional(str)
    create_time = orm.Optional(datetime)
    case_status = orm.Optional(str)
    case_start_time = orm.Optional(datetime)
    case_end_time = orm.Optional(datetime)
    case_deadline_time = orm.Optional(datetime)
    data_source = orm.Optional(str)
    belong = orm.Optional(str)
    team_id = orm.Optional(int)
    update_user = orm.Optional(str)
    process_version = orm.Optional(int)
    invoice_name = orm.Optional(str)


class CaseFileLabel(db.Entity):
    _table_ = 'case_file_label'

    claim_no = orm.Optional(str)
    type = orm.Optional(str)
    second_type = orm.Optional(str)
    provinces = orm.Optional(str)
    file_id = orm.Optional(int)
    is_input = orm.Optional(int)


class File(db.Entity):
    _table_ = 'file'

    id = orm.PrimaryKey(int)
    name = orm.Optional(str)
    url = orm.Optional(str)
    create_time = orm.Optional(datetime)
    rotate = orm.Optional(int)


class CaseImportHi(db.Entity):
    _table_ = 'case_import_hi'

    batch_id = orm.Optional(str)
    claim_no = orm.Optional(str)
    status = orm.Optional(str)
    create_time = orm.Optional(datetime)


class CaseReturnHi(db.Entity):
    _table_ = 'case_return_hi'

    applyno = orm.Optional(str)
    return_result = orm.Optional(str)
    start_time = orm.Optional(datetime)
    end_time = orm.Optional(datetime)


class CaseDataManual(db.Entity):
    _table_ = 'case_data_manual'

    claim_no = orm.Optional(str)
    data = orm.Optional(orm.Json)
    update_time = orm.Optional(datetime)
    create_user = orm.Optional(str)
    update_user = orm.Optional(str)


class CaseDataTask(db.Entity):
    _table_ = 'case_data_task'

    claim_no = orm.Optional(str)
    data = orm.Optional(orm.Json)
    type = orm.Optional(str)
    claim_time = orm.Optional(datetime)


class Batch(db.Entity):
    _table_ = 'batch'

    id = orm.PrimaryKey(str)
    # 1, 太养-微信
    # 2, 太养-柜面
    # 3, 太养-柜面常规
    business_id = orm.Optional(int)
    create_time = orm.Optional(datetime)


class Business(db.Entity):
    _table_ = 'business'

    id = orm.PrimaryKey(str)
    business_name = orm.Optional(str)


class TaiyangOfflineBackData(db.Entity):
    _table_ = 'taiyang_offline_back_data'

    apply_no = orm.Optional(str)
    result = orm.Optional(str)
    create_tm = orm.Optional(datetime)


class TaiyangImageVerify(db.Entity):
    _table_ = 'taiyang_image_verify'

    applyno = orm.Optional(str)
    organid = orm.Optional(str)
    applysource = orm.Optional(str)
    receivedate = orm.Optional(datetime)
    isreceive = orm.Optional(str)

class ErrorRecord(db.Entity):
    _table_ = 'error_record'

    error_date = orm.Optional(datetime)
    claim_no = orm.Optional(str)
    field_name = orm.Optional(str)
    entered_value = orm.Optional(str)
    check_value = orm.Optional(str)
    entered_user = orm.Optional(str)
    group = orm.Optional(str)
    check_user = orm.Optional(str)
    file_name = orm.Optional(str)
    error_type = orm.Optional(str)
    remark = orm.Optional(str)


class AlgFrameworkBill(db.Entity):
    _table_ = 'alg_framework_bill'

    bill_input_person = orm.Required(str)
    result = orm.Required(orm.Json)
    creation_time = orm.Required(datetime)
    error = orm.Required(str)
    bill_code = orm.Required(str)
    bill_number = orm.Required(str)
    bill_check_code = orm.Required(str)
    bill_amount = orm.Required(str)
    bill_date = orm.Required(str)
    bill_payer = orm.Required(str)
    image_path = orm.Required(str)
    claim_no = orm.Required(str)

class YiYuanDingDian(db.Entity):
    _table_ = 'yiyuan_dingdian'

    institution_id = orm.Required(str)
    institution_name = orm.Required(str)
    province = orm.Required(str)
    city = orm.Required(str)


class HospitalMapping(db.Entity):
    _table_ = 'inphile_big_data_hospital_mapping'

    hospital_id = orm.Optional(str)
    hospital_name_seal = orm.Optional(str)
    hospital_name_bill = orm.Optional(str)
    hospital_name_nhsa = orm.Optional(str)


class CaseNewProcessInfo(db.Entity):
    _table_ = 'case_newprocess_info'

    claim_no = orm.Required(str)
    type = orm.Optional(str)
    process_status = orm.Optional(str)

################
# place all entities above
################


# all entities connected to the database must be defined before calling generate_mapping()
# if the tables do not already exist, then they will be created
db.generate_mapping()
